// -------------------------------------------------------------------------
//    @FileName			:    UserBodyDialog.cpp
//    @Author           :    Johance
//    @Date             :    2016-12-28
//    @Module           :    UserBodyDialog
//
// -------------------------------------------------------------------------
#include "NFCUIUtils.h"

#include <ui/UIImageView.h>
#include <2d/CCLayer.h>
#include <2d/CCActionInterval.h>
#include <CCApplication.h>
#include <base/CCDirector.h>

#include "NFComm/NFDBService/DBService.h"

namespace NFCUtils
{
    void CreateuiCombo(Node *pRoot, ui::Button *pButton, const std::vector<std::string> &vecOption, const std::function<void(int nIndex)> &changefun)
    {
        pButton->addClickEventListener([=](Ref *pSender)
        {
            auto pTouchLayer = ui::Layout::create();
            pTouchLayer->setSizeType(ui::Widget::SizeType::PERCENT);
            pTouchLayer->setSizePercent(Vec2(1, 1));
            pRoot->addChild(pTouchLayer);
            pTouchLayer->setTouchEnabled(true);
            pTouchLayer->addClickEventListener([=](Ref* pSender)
            {
                pTouchLayer->removeFromParent();
            });

            ui::ListView *pListView = ui::ListView::create();
            Size contentSize = pButton->getContentSize();
            contentSize.height *= vecOption.size();
            pListView->setContentSize(contentSize);
            pListView->setItemModel(pButton);
            pListView->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
            pListView->setBackGroundColor(Color3B(200, 200, 200));
            for (int i = 0; i < vecOption.size(); i++)
            {
                pListView->pushBackDefaultItem();
                ui::Button *pItem = (ui::Button*)pListView->getItem(i);
                pItem->setTitleText(vecOption[i]);
                pItem->addClickEventListener([=](Ref *pSender)
                {
                    pTouchLayer->setVisible(false);
                    pButton->setTitleText(pItem->getTitleText());
                    pButton->setTag(i);
                    if (changefun)
                        changefun(i);
                });
            }

            pTouchLayer->addChild(pListView);
            Vec2 worldPos = pButton->convertToWorldSpace(Vec2(0, -contentSize.height));
            worldPos = pTouchLayer->convertToNodeSpace(worldPos);
            if (worldPos.y < 0)
            {
                worldPos.y = pButton->getContentSize().height;
            }
            pListView->setPosition(worldPos);
        });
    }

    void CreateuiCombo(Node *pRoot, ui::Text *pButton, const std::vector<std::string> &vecOption, const std::function<void(int nIndex)> &changefun /*= nullptr*/, const std::function<void(Ref*)> btnCallback /*= nullptr*/)
    {
        pButton->setTouchEnabled(true);
        pButton->addClickEventListener([=](Ref *pSender)
        {
            auto pTouchLayer = ui::Layout::create();
            pTouchLayer->setSizeType(ui::Widget::SizeType::PERCENT);
            pTouchLayer->setSizePercent(Vec2(1, 1));
            pRoot->addChild(pTouchLayer);
            pTouchLayer->setTouchEnabled(true);
            pTouchLayer->addClickEventListener([=](Ref* pSender)
            {
                pTouchLayer->removeFromParent();
            });

            ui::ListView *pListView = ui::ListView::create();
            Size contentSize = pButton->getContentSize();
            contentSize.height *= vecOption.size();
            pListView->setContentSize(contentSize);
            pListView->setItemModel(pButton);
            pListView->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
            pListView->setBackGroundColor(Color3B(200, 200, 200));
            for (int i = 0; i < vecOption.size(); i++)
            {
                pListView->pushBackDefaultItem();
                ui::Text *pItem = (ui::Text*)pListView->getItem(i);
                pItem->setString(vecOption[i]);
                pItem->setTouchEnabled(true);
                pItem->addClickEventListener([=](Ref *pSender)
                {
                    pTouchLayer->setVisible(false);
                    pButton->setString(pItem->getString());
                    pButton->setTag(i);
                    if (changefun)
                        changefun(i);
                });
            }

            pTouchLayer->addChild(pListView);
            Vec2 worldPos = pButton->convertToWorldSpace(Vec2(0, -contentSize.height));
            worldPos = pTouchLayer->convertToNodeSpace(worldPos);
            if (worldPos.y < 0)
            {
                worldPos.y = contentSize.height;
            }
            pListView->setPosition(worldPos);

            if (btnCallback)
                btnCallback(pSender);
        });
    }

    void CreateuiWidgetImageTips(Node *pRoot, ui::Widget *pWidget, std::string strImage)
    {
        pWidget->setTouchEnabled(true);
        pWidget->addClickEventListener([=](Ref *pSender)
        {
            auto pTouchLayer = ui::Layout::create();
            pTouchLayer->setSizeType(ui::Widget::SizeType::PERCENT);
            pTouchLayer->setSizePercent(Vec2(1, 1));
            pRoot->addChild(pTouchLayer);
            pTouchLayer->setTouchEnabled(true);
            pTouchLayer->addClickEventListener([=](Ref* pSender)
            {
                pTouchLayer->removeFromParent();
            });

            ui::ImageView *pImageView = ui::ImageView::create(strImage);
            //pImageView->setAnchorPoint(Vec2::ZERO);
            Size contentSize = pImageView->getContentSize();
            pTouchLayer->addChild(pImageView);

            pImageView->setPosition(Vec2(pTouchLayer->getContentSize().width / 2, pTouchLayer->getContentSize().height/2));

            //Vec2 worldPos = pWidget->convertToWorldSpace(Vec2(0, -contentSize.height));
            //worldPos = pTouchLayer->convertToNodeSpace(worldPos);
            //if (worldPos.y < 0)
            //{
            //    worldPos.y = contentSize.height + pWidget->getContentSize().height;
            //}
            //if (worldPos.y > pTouchLayer->getContentSize().height - contentSize.height)
            //{
            //}
            //pImageView->setPosition(worldPos);
        });
    }

    ui::TextField * CreateuiTextField(ui::Text *pText, const std::string &strHoldText /*= ""*/, const ui::TextField::ccTextFieldCallback& callback)
    {
        ui::TextField *pInput = ui::TextField::create();
        pInput->ignoreContentAdaptWithSize(pText->isIgnoreContentAdaptWithSize());
        pInput->setContentSize(pText->getContentSize());
        pInput->setFontSize(pText->getFontSize());
        pText->getParent()->addChild(pInput);
        pInput->setPosition(pText->getPosition());
        pInput->setPlaceHolder(strHoldText);
        pInput->setTextColor(pText->getTextColor());
        pInput->setString(pText->getString());
        pInput->setTouchAreaEnabled(true);
        pInput->setTouchSize(pText->getContentSize());
        pInput->setTextHorizontalAlignment(pText->getTextHorizontalAlignment());
        pInput->setTextVerticalAlignment(pText->getTextVerticalAlignment());

        pText->setOpacity(0);

        pInput->addEventListener([=](Ref* pSender, ui::TextField::EventType type)
        {
            if (type == ui::TextField::EventType::ATTACH_WITH_IME)
            {
                pText->setVisible(true);
                pInput->setString(pText->getString());
#ifdef _WIN32
                LayerColor *pCursor = LayerColor::create(Color4B::WHITE, 2, pInput->getFontSize()*1.1);
                pCursor->setAnchorPoint(Vec2(0.5, 0.5));
                pCursor->ignoreAnchorPointForPosition(false);
                pInput->addChild(pCursor);
                pCursor->setName("cursor");
                pCursor->runAction(RepeatForever::create(Blink::create(1, 1)));
                Vec2 pos = Vec2(pInput->getAutoRenderSize().width, pInput->getContentSize().height / 2.0f);
                if (pInput->getString().size() == 0)
                {
                    pos.x = pInput->getAutoRenderSize().width / 2.0f;
                }
                if (pos.x > pInput->getContentSize().width)
                    pCursor->setOpacity(0);
                else
                    pCursor->setOpacity(255);
                pCursor->setPosition(pos);
#endif
            }
            else if (type == ui::TextField::EventType::DETACH_WITH_IME)
            {
                pText->setString(pInput->getString());
#ifdef _WIN32
                pInput->removeChildByName("cursor");
#endif
            }
#ifdef _WIN32
            else if (type == ui::TextField::EventType::DELETE_BACKWARD || type == ui::TextField::EventType::INSERT_TEXT)
            {
                auto pCursor = pInput->getChildByName("cursor");
                Vec2 pos = Vec2(pInput->getAutoRenderSize().width, pInput->getContentSize().height / 2.0f);
                if (pInput->getString().size() == 0)
                {
                    pos.x = pInput->getAutoRenderSize().width / 2.0f;
                }

                pCursor->setPosition(pos);
                pText->setString(pInput->getString());
            }
#endif

            if (callback)
                callback(pSender, type);
        });

        return pInput;
    }

    void CreateuiListModel(ui::ListView *pListView)
    {
        if (pListView->getItems().size() == 0)
            return ;

        ui::Widget *pWidget = dynamic_cast<ui::Widget*>(pListView->getItem(0));
        if (!pWidget)
            return;

        pListView->setItemModel(pWidget);
        pListView->removeItem(0);
    }

    void UpdateStringToControl(ui::Button *pText, const std::string &strKey)
    {
        static LanguageType type = Application::getInstance()->getCurrentLanguage();
        int nTableType;
        switch (type)
        {
            case LanguageType::CHINESE:
                nTableType = TT_TEXT_CN;
            default:
                nTableType = TT_TEXT_EN;
        }
        DBData_text_cn *pData = (DBData_text_cn *)g_pDBService->FindData(TT_TEXT_EN, strKey.c_str());
        if (pData)
        {
            pText->setTitleText(pData->text);
        }
    }

    void SetDesignResolutionSize(Size minSize, Size maxSize)
    {
        Size screenSize = Director::getInstance()->getOpenGLView()->getFrameSize();
        float fHeight = screenSize.height;
        float fWidth = screenSize.width;

        float fScale = 1.0f;

        if (fHeight > maxSize.height || fWidth > maxSize.width)
        {
            fScale = min(maxSize.height / fHeight, maxSize.width / fWidth);
        }
        else if (fHeight < minSize.height || fWidth < minSize.width)
        {
            fScale = max(minSize.height / fHeight, minSize.width / fWidth);
        }

        Director::getInstance()->getOpenGLView()->setDesignResolutionSize(fScale*fWidth, fScale*fHeight, ResolutionPolicy::NO_BORDER);
    }
}